<?php
include("config/connect.php");
include("public/common.php");
$phone = trim(CheckUrlParamNoDie("phone_id"));
$tb_id = trim(CheckUrlParamNoDie("alipay_id"));
$commend_id = trim(CheckUrlParamNoDie("commend_id"));
$commend_id = GetRecommendIdFromCode($commend_id, $type);

//$email = CheckUrlParamNoDie("email_id");
$opt = CheckUrlParamNoDie( "opt" );
$use_phone = CheckUrlParamNoDie( "use_phone" );

$sql = "select id from user where tb_id='$tb_id' limit 1";
$result = mysql_query( $sql );
$row_num = mysql_num_rows( $result );
if( $row_num == 0 )
{
	$result2 = mysql_query( "select type from user where id=$commend_id limit 1" );
	$row = mysql_fetch_assoc( $result2 );
	$type = strtoupper($row["type"]);
	if ($type == 'U' || $type == 'Y')
		$type = 'N';
	else if ($type == 'V')
		$type = 'Y';
	else
		$type = 'N';
	
	if( $use_phone == "1" )
		$sql = "insert into user(tb_id, phone, email, commend_id, type) values('$tb_id', '$phone', '$tb_id', '$commend_id', '$type')";
	else 
		$sql = "insert into user(tb_id, phone, email, commend_id, type) values('$tb_id', '$tb_id', '$phone', '$commend_id', '$type')";
		
	$result = mysql_query( $sql );
	$userid = mysql_insert_id();
	
	// 消息
	if ($commend_id=="ICBC95588")
	{
		mysql_query( "update user set level=level+100,tubi=tubi+100 where id=$userid" );
		mysql_query( "insert into gwt_msg (touser,content) values ($userid,'尊敬的 ".$tb_id."，恭喜您已成功注册购物兔，您是工行用户，额外赠送您200兔币，祝您购物愉快！')" );
	}
	else
		mysql_query( "insert into gwt_msg (touser,content) values ($userid,'尊敬的 ".$tb_id."，恭喜您已成功注册购物兔，赠送您100兔币，祝您购物愉快！')" );

	
	// 临时往活动里插入一条数据
	if ($commend_id != "")
	{
		$result = mysql_query( "select id from hd_lottery where user_id=$commend_id limit 1" );
		if(mysql_num_rows( $result ) > 0)	// 判断用户是否参加活动
		{
			$rndNum = rand(0000000,9999999);
			$result = mysql_query( "insert into hd_lottery (user_id,invitees_id,lottery_num) values ($commend_id, $userid, '$rndNum')" );
		}
	}
}
else
{
	if( $use_phone == "1" )
	{
		if( $phone != "" )
			$sql = "update user set phone='$phone', email='$tb_id' where tb_id='$tb_id'";
		else
			$sql = "update user set email='$tb_id' where tb_id='$tb_id'";
	}
	else
	{
		if( $phone != "" )
			$sql = "update user set email='$phone', phone='$tb_id' where tb_id='$tb_id'";
		else
			$sql = "update user set phone='$tb_id' where tb_id='$tb_id'";
	}
	$result = mysql_query( $sql );
}

// 要返回UserID
$result2 = mysql_query( "select id,phone from user where tb_id='$tb_id' limit 1" );
$row = mysql_fetch_assoc( $result2 );
$url = "Location:register.html?hr=1&userid=".$row["id"]."&tbid=$tb_id&phone=".$row["phone"];
header($url);
?>